package top.birdhk.TestAPI.transorm;


import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 *
 */
public class Transform_Base {

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> inputSream = env.readTextFile("E:\\flink\\wordcount\\src\\main\\resources\\sensor.txt");


        // 1. map 把String转换成长度输出

        SingleOutputStreamOperator<Integer> map = inputSream.map(new MapFunction<String, Integer>() {

            @Override
            public Integer map(String s) throws Exception {
                return s.length();
            }

        });

        // 2. flatMap 按照规则切分
        SingleOutputStreamOperator<String> flatMap = inputSream.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] split = s.split(",");
                for (String s1 : split) {
                    collector.collect(s1);
                }
            }
        });


        // 3 filter 筛选过滤
        SingleOutputStreamOperator<String> filter = inputSream.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return s.startsWith("s");
            }
        });


        inputSream.print();


        map.print();


        flatMap.print();

        filter.print();

        env.execute();


    }


}
